package com.thti.mecool.client.widget;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.TextView;

import com.thti.mecool.client.activity.R;
import com.thti.mecool.client.activity.TaskActivity;
import com.thti.mecool.client.activity.TaskExecute;
import com.thti.mecool.client.adapter.MutilListAdapter;

public class ListWidget {
	ExpandableListView TaskListView;
	Activity activity;
	ArrayList<ArrayList<HashMap<String, String>>> taskList;
	ArrayList<String> listTag;

	ArrayList<ArrayList<HashMap<String, String>>> donetaskList;
	ArrayList<String> donelistTag;
	MutilListAdapter mutilAdapter;
	MutilListAdapter SyncTaskAdapter;
	ExpandableListView DoneTaskList;
	ArrayList<ArrayList<Integer>> stat;
	ExpandableListView taskundonelist;
	MutilListAdapter undone_detail_adapter;
	private ArrayList<String> curProjectIds, PlanIds, StoreNames, StoreCodes,
			taskIds, taskNames;
	int curGroup = 0;
	int curChild = 0;

	public ListWidget(Activity activity,
			ArrayList<ArrayList<HashMap<String, String>>> taskList,
			ArrayList<String> listTag) {
		this.activity = activity;
		this.taskList = taskList;
		this.listTag = listTag;
		listinit();
	}

	public void setTaskListData(
			ArrayList<ArrayList<HashMap<String, String>>> taskList,
			ArrayList<String> listTag) {
		this.taskList = taskList;
		this.listTag = listTag;
		mutilAdapter.notifyDataSetChanged();
	}

	public ListWidget(Activity activity,
			ArrayList<ArrayList<HashMap<String, String>>> taskList,
			ArrayList<String> listTag, ArrayList<ArrayList<Integer>> stat) {
		this.activity = activity;
		this.donetaskList = taskList;
		this.donelistTag = listTag;
		this.stat = stat;
		listOtherInit();
	}

	OnGroupClickListener TaskListOnChildListener=new OnGroupClickListener() {
		
		@SuppressWarnings("unused")
		@Override
		public boolean onGroupClick(ExpandableListView parent, View v,
				int groupPosition, long id) {
			TextView Taskname = (TextView) v
					.findViewById(R.id.mecool_tasklist_itemname);
			curGroup = groupPosition;
			curChild = 0;
			Intent i = new Intent(activity, TaskExecute.class);
			i.putExtra("taskName", taskNames.get(groupPosition));
			i.putExtra("storeName", StoreNames.get(groupPosition));
			i.putExtra("storeCode", StoreCodes.get(groupPosition));
			i.putExtra("planId", PlanIds.get(groupPosition));
			i.putExtra("projectId", curProjectIds.get(groupPosition));
			i.putExtra("taskId", taskIds.get(groupPosition));
			activity.startActivityForResult(i, Activity.RESULT_OK);
			return false;
		}
	};
	
	private void listOtherInit(){
		if (activity.getClass().equals(TaskActivity.class)) {
			DoneTaskList = (ExpandableListView) activity
					.findViewById(R.id.mecool_done_task);
			SyncTaskAdapter = new MutilListAdapter(donetaskList, donelistTag,
					stat, activity);
			DoneTaskList.setAdapter(SyncTaskAdapter);
//			if (donelistTag != null)
//				donelistExtra(donelistTag);
			donelistRefurbish();
		}
	}
	private void listinit() {
		if (activity.getClass().equals(TaskActivity.class)) {
			TaskListView = (ExpandableListView) activity
					.findViewById(R.id.undone_task);
			mutilAdapter = new MutilListAdapter(taskList, listTag, activity);
			TaskListView.setAdapter(mutilAdapter);
			TaskListView.setOnGroupClickListener(TaskListOnChildListener);
//			if (listTag != null)
//				listExtra(listTag);
			listRefurbish();
		} else {
			taskundonelist = (ExpandableListView) activity
					.findViewById(R.id.mecool_mutil_list);
			undone_detail_adapter = new MutilListAdapter(taskList, listTag,
					activity);
			taskundonelist.setAdapter(undone_detail_adapter);
//			otherlistExtra(listTag);
			OtherListRefurbish();
		}
	}

	public void setPlanIdAndProjectIds(ArrayList<String> planIds,
			ArrayList<String> projectIds, ArrayList<String> storeCodes,
			ArrayList<String> storeNames, ArrayList<String> taskIds,
			ArrayList<String> taskNames) {
		this.taskNames = taskNames;
		this.PlanIds = planIds;
		this.curProjectIds = projectIds;
		this.StoreNames = storeNames;
		this.StoreCodes = storeCodes;
		this.taskIds = taskIds;
	}

	public void delData(int curPos) {
		taskList.remove(curPos);
		listTag.remove(curPos);
		ArrayList<ArrayList<HashMap<String, String>>> TempArrList = new ArrayList<ArrayList<HashMap<String, String>>>();
		ArrayList<String> TempTagList = new ArrayList<String>();
		TempArrList.addAll(taskList);
		TempTagList.addAll(listTag);
		taskList.clear();
		listTag.clear();
		for (ArrayList<HashMap<String, String>> list : TempArrList) {
			taskList.add(list);
		}
		for (String s : TempTagList) {
			listTag.add(s);
		}
		OtherListRefurbish();
	}

	public ExpandableListView getotherlist() {
		return taskundonelist;
	}
public MutilListAdapter getOtherAdapter(){
	return undone_detail_adapter;
}
	public void OtherListRefurbish() {
		undone_detail_adapter.notifyDataSetChanged();
	}

	public void listRefurbish() {
		if (mutilAdapter != null)
			mutilAdapter.notifyDataSetChanged();
	}

	public void donelistRefurbish() {
		if (SyncTaskAdapter != null)
			SyncTaskAdapter.notifyDataSetChanged();
	}

	public void setStat(ArrayList<ArrayList<Integer>> stat2) {
		SyncTaskAdapter.setStat(stat2);
	}

	/**
	 * 本功能用于关闭收缩功能以及自动展开所有列表项
	 * 
	 * @param listTag
	 */
//	public void listExtra(ArrayList<String> listTag) {
//		if (!listTag.isEmpty()) {
//			if (activity.getClass().equals(TaskActivity.class)) {
//				for (int i = 0; i < listTag.size(); i++) {
//					TaskListView.expandGroup(i);
//				}
//				TaskListView
//						.setOnGroupClickListener(new ForbidGroupClickListener());
//			}
//		}
//	}
//
//	public void donelistExtra(ArrayList<String> listTag) {
//		if (!donelistTag.isEmpty()) {
//			if (activity.getClass().equals(TaskActivity.class)) {
//				for (int i = 0; i < donelistTag.size(); i++) {
//					DoneTaskList.expandGroup(i);
//				}
//				DoneTaskList
//						.setOnGroupClickListener(new ForbidGroupClickListener());
//			}
//		}
//	}
//
//	public void otherlistExtra(ArrayList<String> listTag) {
//		if (!listTag.isEmpty()) {
//
//			for (int i = 0; i < listTag.size(); i++) {
//				taskundonelist.expandGroup(i);
//			}
//			taskundonelist
//					.setOnGroupClickListener(new ForbidGroupClickListener());
//		}
//	}
}
